import java.util.ArrayList;
import java.util.List;

public class Test {
    static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    List<String> ret = new ArrayList<>();

    public List<String> binaryTreePaths(TreeNode root) {
        dfs(root,new StringBuffer());
        return ret;
    }

    public void dfs(TreeNode root, StringBuffer path) {
        if(root == null) return;
        path.append(root.val);
        if(root.left == null && root.right == null) {
            ret.add(path.toString());
            return;
        }
        path.append("->");
        dfs(root.left, path);
        dfs(root.right, path);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode root2 = new TreeNode(2);
        TreeNode root3 = new TreeNode(3);
        TreeNode root5 = new TreeNode(5);
        root.left = root2;
        root.right = root3;
        root2.right = root5;

        Test test = new Test();
        test.binaryTreePaths(root);
    }
}
